﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServBus.ListForm2010.Setup.Commands;
using ServBus.ListForm2010.Setup.Controls;
using ServBus.ListForm2010.Setup.Validators;

namespace ServBus.ListForm2010.Setup.SubForms
{
    public partial class CheckForm : BaseStepForm
    {
        private System.Timers.Timer timer;
        private List<ValidationControl> validationControls;
        private int currentValidator;
        private delegate void AllValidationFinishedHandler();
        private SolutionStatus solutionStatus;

        public CheckForm()
        {
            InitializeComponent();
        }

        public CheckForm(BaseStepForm previous, SetupForm setupForm)
            : base(previous, setupForm)
        {
            InitializeComponent();
        }

        public override string Title
        {
            get
            {
                return Properties.Resources.CheckTitle;
            }
        }

        public override string SubTitle
        {
            get
            {
                return Properties.Resources.CheckSubTitle;
            }
        }

        public override void Init()
        {
            this.ParentSetupForm.PrevButton.Enabled = true;
            this.ParentSetupForm.NextButton.Enabled = true;
        }

        public override void Execute()
        {
            timer = new System.Timers.Timer(500);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            AddValidations();

            this.labelInfo.Text = Properties.Resources.CheckingInfo;
            this.ParentSetupForm.PrevButton.Enabled = false;
            this.ParentSetupForm.NextButton.Enabled = false;
            this.ParentSetupForm.AbortButton.Enabled = false;
            timer.Start();
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer.Stop();
            this.currentValidator++;
            if (this.currentValidator == this.validationControls.Count ||
                this.currentValidator == 1 && !this.validationControls[0].Validator.Validated)
            {
                this.BeginInvoke(new AllValidationFinishedHandler(CheckFinished));
            }
            else
            {
                this.validationControls[this.currentValidator].RunValidate();
            }
        }

        private void AddValidation(BaseValidator validator)
        {
            ValidationControl ctrl = new ValidationControl(validator, timer);
            this.flowLayout.Controls.Add(ctrl);
            this.validationControls.Add(ctrl);
        }

        private void AddValidations()
        {
            this.currentValidator = -1;
            this.flowLayout.Controls.Clear();
            this.validationControls = new List<ValidationControl>();
            //AddValidation(new OSValidator());
            AddValidation(new SharePointValidator());
            AddValidation(new PermissionValidator());
            AddValidation(new SPAdminValidator());
            AddValidation(new TimerValidator());
            AddValidation(new SolutionValidator());
        }

        private void CheckFinished()
        {
            bool allPass = true;
            foreach (ValidationControl ctrl in this.validationControls)
            {
                if (!ctrl.Validator.Validated)
                {
                    allPass = false;
                    break;
                }
            }

            this.ParentSetupForm.AbortButton.Enabled = true;
            if (allPass)
            {
                this.solutionStatus = (this.validationControls[4].Validator as SolutionValidator).Status;
                if (this.solutionStatus == SolutionStatus.None)
                {
                    this.labelInfo.Text = Properties.Resources.CheckPassInstallInfo;
                }
                else
                {
                    this.labelInfo.Text = Properties.Resources.CheckPassInfo;
                }
                this.ParentSetupForm.PrevButton.Enabled = true;
                this.ParentSetupForm.NextButton.Enabled = true;
                this.ParentSetupForm.NextButton.Text = Properties.Resources.NextButton;
                this.ParentSetupForm.NextButton.Click -= new EventHandler(this.ParentSetupForm.btnNext_Click);
                this.ParentSetupForm.NextButton.Click -= new EventHandler(RetryCheck);
                this.ParentSetupForm.NextButton.Click += new EventHandler(this.ParentSetupForm.btnNext_Click);
            }
            else
            {
                this.labelInfo.Text = Properties.Resources.CheckFailInfo;
                this.ParentSetupForm.PrevButton.Enabled = false;

                this.ParentSetupForm.NextButton.Enabled = true;
                this.ParentSetupForm.NextButton.Text = Properties.Resources.RetryButton;
                this.ParentSetupForm.NextButton.Click -= new EventHandler(this.ParentSetupForm.btnNext_Click);
                this.ParentSetupForm.NextButton.Click -= new EventHandler(RetryCheck);
                this.ParentSetupForm.NextButton.Click += new EventHandler(RetryCheck);
            }
        }

        private void RetryCheck(object sender, EventArgs e)
        {
            foreach (ValidationControl ctrl in this.validationControls)
                ctrl.ResetControlState();
            this.currentValidator = -1;
            timer.Start();
        }

        public override BaseStepForm CreateNextStep()
        {
            if (this.solutionStatus == SolutionStatus.None)
            {
                return new OperationForm(this, this.ParentSetupForm, InstallBatch.Install);
            }
            else
            {
                return new ChooseBatchForm(this, this.ParentSetupForm, this.solutionStatus);
            }
        }
    }
}
